#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>
#include <string.h>
#include "popupmenu.h"
#include "clist.h"
#include "mainwindow.h"
#include "addgroup.h"
#include "find.h"
#include "newinfo.h"
/*
 * callback for popup menu item '打开链接' to handle clicked singal
 */
void
on_popup_item_open_link_activate       (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	struct clist_struct *clist=(struct clist_struct *)user_data;
	#ifdef _DEBUG_
		g_print("on_popup_item_open_link_activate :-----\n");
	#endif
	
	if(clist->show_all_row>0 && clist->show_selected_row != -1) {
		gchar *link;
		gtk_clist_get_text(GTK_CLIST(clist->clist_show),
				clist->show_selected_row,4,&link);
		if(strlen(link)==0||strcmp(link,"---")==0) {
			gchar msg[]="选中的信息博客记录为空!";
			message_box_for_toolbar(window_main,msg);
			return;
		}
		gchar blog[100];
		g_snprintf(blog,100,"firefox %s & >/dev/null",link);
		system(blog);
	}
}
/*
 * callback for popup menu item '新建信息' to handle clicked singal
 */
void
on_popup_item_new_info_activate        (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	handle_for_create_new_info(user_data);
}
/*
 * callback for popup menu item '删除信息' to handle clicked singal
 */
void
on_popup_item_delete_info_activate     (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	/*
	 * delete_info_from_clist_show:defined in clist.c.
	 * @user_data:       struct newinfo_struct
	 */
	delete_info_from_clist_show(user_data);
}
/*
 * callback for popup menu item '复制链接' to handle clicked singal
 */
void
on_popup_item_copy_all_activate       (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	struct clist_struct *clist=(struct clist_struct *)user_data;
	GtkClipboard* clipboard;
	gchar *info[MAX_COLUMNS];
	gchar text[80];
	
	if(clist->show_selected_row != -1) {
		gtk_clist_get_text(GTK_CLIST(clist->clist_show),clist->show_selected_row,0,&info[0]);
		gtk_clist_get_text(GTK_CLIST(clist->clist_show),clist->show_selected_row,1,&info[1]);
		gtk_clist_get_text(GTK_CLIST(clist->clist_show),clist->show_selected_row,2,&info[2]);
		gtk_clist_get_text(GTK_CLIST(clist->clist_show),clist->show_selected_row,3,&info[3]);
		gtk_clist_get_text(GTK_CLIST(clist->clist_show),clist->show_selected_row,4,&info[4]);
		g_snprintf(text,80,"%s %s %s %s %s",info[0],info[1],info[2],info[3],info[4]);
		clipboard=gtk_widget_get_clipboard(window_main, GDK_SELECTION_CLIPBOARD);
		gtk_clipboard_set_text(clipboard, text, strlen(text));
	} else {
		gchar msg[]="当前分组没有信息!";
		message_box_for_toolbar(window_main,msg);
	}
}                                        
void
on_popup_item_copy_blog_activate       (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	struct clist_struct *clist=(struct clist_struct *)user_data;
	GtkClipboard *clipboard;
	gchar *text;
	
	if(clist->show_selected_row != -1) {
		gtk_clist_get_text(GTK_CLIST(clist->clist_show),clist->show_selected_row,4,&text);
		if(strcmp(text,"---")==0) {
			gchar msg[]="选中的信息博客记录为空!";
			message_box_for_toolbar(window_main,msg);
			return;
		}
		clipboard=gtk_widget_get_clipboard(window_main, GDK_SELECTION_CLIPBOARD);
		gtk_clipboard_set_text(clipboard, text, strlen(text));
	} else {
		gchar msg[]="选中的信息博客记录为空!";
		message_box_for_toolbar(window_main,msg);
	}
}                                        
/*
 * callback for popup menu item '查找信息' to handle clicked singal
 */
void
on_popup_item_find_info_activate       (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	handle_for_create_window_find(user_data);
}

void
on_popup_item_update_info_activate       (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	 struct newinfo_struct *newinfo=(struct newinfo_struct *)user_data;
	 newinfo->window=(GtkWidget *)create_window_update_info(newinfo,newinfo->clist);
	 gtk_widget_show(newinfo->window);
}
/*
 * callback for popup menu item '添加分组' to handle activte singal
 */
void
on_popup_menu_for_add_group_activate  (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	handle_for_create_window_add(user_data);//defined in addgroup.h
}


void
on_popup_menu_for_delete_group_activate(GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	delete_info_from_clist_group(user_data);//defined in cclist.c
}

void
on_popup_menu_for_update_group_activate(GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	 struct addgroup_struct *addgroup=(struct addgroup_struct *)user_data;
	 addgroup->window=(GtkWidget *)create_window_update_group(addgroup,addgroup->clist);
	 gtk_widget_show(addgroup->window);
}

